#include "pch.h"
#include "Handle.h"
#include "../Behavior.h"

void LoadFilePaths()
{
    const std::string filePath = "../../";
    fs::path p1(filePath);

    const std::vector<std::string> whiteAlbum = {"Behavior.h", "Export.h"};

    for (const auto &child : fs::directory_iterator(p1))
    {
        std::string childFileName = child.path().filename().string();

        // LOGI("child file name %s", childFileName.c_str());
        if (std::find(whiteAlbum.begin(), whiteAlbum.end(), childFileName) == whiteAlbum.end())
        {
            LOGI("get a behavior whic filename is %s ", childFileName.c_str());
        }
    }
};

namespace Hag
{
    struct BehaviorManager
    {
        std::string m_SharedPath;
        void LoadShared()
        {
        }

        template <typename T>
        T GetEntryFunction(const std::string &fnName)
        {
        }
    };
};

int main()
{

    LoadFilePaths();
    // Must be aboslute path
    const std::string shareLibPath = "../../build/libbehavior.so";

    Shared::LibHandle handle;

    if (!handle.Open(shareLibPath.c_str()))
    {
        LOGE("failed Open Shared lib from path %s", shareLibPath.c_str());
        return 0;
    }
    LOGI("Handle Open successfully");

    typedef Hag::Behavior *(*MyFunc)(const std::string &path);

    MyFunc func = handle.GetProcAddrAs<MyFunc>("GetBehavior");

    while (1)
    {
        std::string fileName;
        std::cin >> fileName;

        Hag::Behavior *res = func(fileName);

        res->OnStart();
        res->OnUpdate();
        res->OnDestroy();
    }

    handle.Close();
};
